package chenji.watcher.core;

import java.util.HashMap;

import org.bukkit.GameMode;
import org.bukkit.entity.Player;

public class WatchGroupManager {
	private HashMap<String, WatchGroupData> data = new HashMap<>();
	private HashMap<Player, PlayerWatchGroup> task = new HashMap<>();

	public WatchGroupData getWatchGroupData(String Name) {
		return this.data.get(Name);
	}

	public void setWatchGroupData(WatchGroupData wgd) {
		this.data.put(wgd.getName(), wgd);
	}

	public void play(Player p, WatchGroupData wgd) {
		if (!(wgd.canPlay())) {
			p.sendMessage("§a[Watcher] &c对不起，暂时无法播放。观看组内至少含有一个观看传送点。");
			return;
		}
		PlayerWatchGroup pwg = new PlayerWatchGroup(p, wgd);
		this.task.put(p, pwg);
	}

	public void Task() {
		for (Player p : task.keySet()) {
			this.task.get(p).task();
		}

	}

	public void playerQuit(Player p) {
		
		if (this.task.containsKey(p)) {
			this.task.get(p).exit();
			p.setGameMode(GameMode.SURVIVAL);
		}
		this.task.remove(p);
	}

	public void playFinish(Player p) {
		p.setGameMode(GameMode.SURVIVAL);
		this.task.remove(p);
	}

	public boolean contain(Player player) {
		return this.task.containsKey(player);
	}

}
